
package ogame;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import ogame.packages.Costs;
import ogame.packages.FlightTimes;
import ogame.packages.OgameComponent;
import ogame.packages.RaidSelector;
import ogame.utilities.Constants;

/**
 *
 * @author scchang
 */
public class Ogame extends JPanel implements ActionListener{
    
    protected String[] modeStrings = { "Cost Calculation" , "Fight Simulator",
         "Flight Time/Cost", "Raid Selector"};
    protected final int COST_CALCULATION_INDEX = 0;
    protected final int FIGHT_SIMULATOR_INDEX = 1;
    protected final int FLIGHT_TIME_INDEX = 2;
    protected final int RAID_SELECTOR_INDEX = 3;
    protected OgameComponent[] modeComponents;
    
    protected JLabel current_panel;
    protected JComboBox modeList;
    
    protected OgameComponent currentComponent;
    
    protected GridBagLayout gridbagMain;
    protected GridBagConstraints gridbagMainConstraints;

    protected void addComp(JComponent comp, GridBagLayout gridbag, GridBagConstraints c){
        this.addComp(comp, gridbag, c, this.getComponentCount());
    }
    
    protected void addComp(JComponent comp, GridBagLayout gridbag, GridBagConstraints c, int index){
        gridbag.setConstraints(comp, c);
        this.add(comp, index);
    }
    
    public Ogame(){
        this.setPreferredSize(new Dimension(Constants.DEFAULT_WINDOW_WIDTH,
                Constants.DEFAULT_WINDOW_HEIGHT));
        this.setFocusable(true);
        
        this.modeComponents = new OgameComponent[modeStrings.length];
        this.modeComponents[COST_CALCULATION_INDEX] = new Costs(this);
        this.modeComponents[FLIGHT_TIME_INDEX] = new FlightTimes(this);
        this.modeComponents[RAID_SELECTOR_INDEX] = new RaidSelector(this);
        
        // The following is the desired layout
        // Current screen
        // Cycle
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        this.setLayout(gridbag);

        c.weightx = 1.0;
        c.fill = GridBagConstraints.BOTH;
        c.gridwidth = GridBagConstraints.REMAINDER;
        
        // Add current panel
        this.current_panel = new JLabel("Default", JLabel.CENTER);
        this.current_panel.setToolTipText("This is the label for the current panel.");
        this.addComp(this.current_panel, gridbag, c);

        // Add cycler 
        this.modeList = new JComboBox(modeStrings);
        this.modeList.addActionListener(this);
        this.modeList.setEditable(false); // Uneditable
        this.addComp(this.modeList, gridbag, c);
        
        // Default is Costs
        this.selectCosts(gridbag, c);
        
        this.gridbagMain = gridbag;
        this.gridbagMainConstraints = c;
    }
    
    private void selectCosts(GridBagLayout gridbag, GridBagConstraints c){
        this.current_panel.setText("Cost Calculator");
        if(this.getComponents().length > 2){
            this.remove(this.currentComponent);
        }
        this.currentComponent = this.modeComponents[COST_CALCULATION_INDEX];
        this.addComp(this.currentComponent, gridbag, c);
    }
    
    private void selectFlightTimes(GridBagLayout gridbag, GridBagConstraints c){
        this.current_panel.setText("Flight Time Calculator");
        if(this.getComponents().length > 2){
            this.remove(this.currentComponent);
        }
        this.currentComponent = this.modeComponents[FLIGHT_TIME_INDEX];
        this.addComp(this.currentComponent, gridbag, c);
    }
    
    private void selectRaidSelector(GridBagLayout gridbag, GridBagConstraints c){
        this.current_panel.setText("Raid Selector");
        if(this.getComponents().length > 2){
            this.remove(this.currentComponent);
        }
        this.currentComponent = this.modeComponents[RAID_SELECTOR_INDEX];
        this.addComp(this.currentComponent, gridbag, c);
    }
   
    public void actionPerformed(ActionEvent e){
        String event = e.getActionCommand();
        
        if(event.compareTo("comboBoxChanged") == 0){
            System.out.println("Combo Box Changed.");
            int index = this.modeList.getSelectedIndex();
            if(index == COST_CALCULATION_INDEX){
                selectCosts(this.gridbagMain, gridbagMainConstraints);
            }else if(index == FLIGHT_TIME_INDEX){
                selectFlightTimes(gridbagMain, gridbagMainConstraints);
            }else if(index == RAID_SELECTOR_INDEX){
                selectRaidSelector(gridbagMain, gridbagMainConstraints);
            }
        }
        repaint();
    }
}
